package com.tianqu.tms.www.member.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;

/**
 * 
 * 账户信息查看Controller
 * 
 * @author tianshifu
 * 
 */
public class MemberUDAccDetailController extends TmsController {

	private static final String MEMBERID = "memId";// 会员ID

	private static final String ACCOUNTDATA = "AccountData";// 账户信息

	private Logger logger = Logger.getLogger(MemberUDAccDetailController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			String memberId = (String) uc.getAttribute(MEMBERID);

			Member entity = Member.getInstance();

			entity = entity.findMemberInfoByMemId(memberId);

			// 将保证金 虚拟保证金 会员ID打到前台

			MemberInputViewHelper view = new MemberInputViewHelper();

			view.setMargin(StringUtil.addDH(String.valueOf(entity.getMargin())));// 保证金
			
			view.setVirtualMargin(StringUtil.addDH(String.valueOf(entity.getVirtualMargin())));// 虚拟保证金
			view.setMemberId(String.valueOf(entity.getMemId()));// 会员ID

			uc.removeAttribute(ACCOUNTDATA);
			uc.setAttribute(ACCOUNTDATA, view);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
